$(function () {
    queryTrollyNum();
   $.ajax({
       type : "post",
       url : "htmlWeb/trolleyManage",
       async: false,
       success : function(data){
           var str = "";
           $.each(data.r,function(i,obj){
               obj = trimEmpty(obj);
               var scheduleDate = formatDate(obj.scheduleDate,"yyyy-MM-dd");
               var despoit = (obj.product.currentPrice - 0 ) * (obj.product.deposit - 0)/100;
               str += '<tr><input type="hidden" class="trolleyId" value="' + obj.id + '"><td>&nbsp;</td><td><label><input type="checkbox" name="subBox" class="selectDiv" /></label></td>' +
                   '<td><a class="cartImg" href="#"><img src=http://img.boyitongcheng.com/' + obj.user.avatar + '></a></td>' +
                   // '<td><a class="cartImg" href="#"><img src=../../../../by-webhtml/assets/image/temp/img3.jpg></a></td>' +
                   '<td><a class="cartSj" href="#">' + obj.user.cnName + '</a></td>' +
                   '<td><span>' + obj.product.name + '</span></td>' +
                   '<td><b class="cartCurrent">￥' + obj.product.currentPrice + '</b></td>' +
                   '<td><b class="cartDeposit">￥' + despoit + '</b></td>' +
                   '<td><span>' + scheduleDate + '</span></td>' +
                   '<td><span>' + obj.dinnerType + '</span></td>' +
                   '<td><span>' + obj.amount + '</span></td>' +
                   '<td><span>' + obj.descn + '</span></td>' +
                   '<td><em><a href="javascript:void(0);" onclick="deleteTrolley(' + obj.id + ')">删除</a></em></td></tr>';
           });

           $("#trolleyTableHead").after(str);
           addCheckEvent();

       },
       error : function(data){
           console.log(data);
       }
   });

});

function deleteTrolley(id){
    $.post("htmlWeb/deleteTrolley",{"trolley_id":id},function(data, textStatus, jqXHR){
        alert("删除成功");
        navPills("cartHtml");
    });
}

//添加复选框点击事件
function addCheckEvent(){
    //全选
    $("#checkAll").click(function(){
        if(this.checked){
            $("th label").addClass("on");
            $("td label").addClass("on");
            $("td :checkbox[name='subBox']").prop("checked", true);
            $(".stores em").text($('.selectDiv').length);
        }else{
            $("th label").removeClass("on");
            $("td label").removeClass("on");
            $("td :checkbox[name='subBox']").prop("checked", false);
            $(".stores em").text(0);
        }
        sumPrice();
    });
    //单选
    $(".selectDiv").click(function(){
        sumPrice();
        if(this.checked){
            $(this).parent().addClass("on");
        }else{
            $(this).parent().removeClass("on");
        }
        if ($('.selectDiv').length == $('.selectDiv:checked').length) {
            $('#checkAll').prop({
                checked : true
            });
            $("th label").addClass("on");

        } else {
            $('#checkAll').prop({
                checked : false
            });
            $("th label").removeClass("on");

        }
    });
    $(".selectTotalPrice").click(function(){
        $(".cartLa").removeClass("on");
        if(this.checked){
            $(this).parent().addClass("on");
        }

    });
    $(".selectPayType").click(function(){
        $(".cartPayType").removeClass("on");
        if(this.checked){
            $(this).parent().addClass("on");
        }

    });
}

//计算总价
function sumPrice(){
    var cartCurrent = 0,cartDeposit = 0,ids = "",num = 0;
    $("td :checkbox[name='subBox']").each(function(i,obj){
        if(this.checked){
            ++num;
            ids += $(this).parents("tr").find(".trolleyId").val() + ',';
            var cartCurrentNode = $(this).parents("tr").find(".cartCurrent").html().split("￥");
            if(cartCurrentNode.length > 1){
                cartCurrent += cartCurrentNode[1] - 0;
            }
            var cartDepositNode = $(this).parents("tr").find(".cartDeposit").html().split("￥");
            if(cartCurrentNode.length > 1){
                cartDeposit += cartDepositNode[1] - 0;
            }
        }
    });
    $("#cartCurrentTotal").html(cartCurrent);
    $("#cartDepositTotal").html(cartDeposit);
    $("#trolleyIds").val(ids);
    $("#cartBizTotal").html(num);
}

/**
 * 结算 提交订单
 * @param payType
 */
function submitOrder(){
    var ids = $("#trolleyIds").val();
    if(typeof ids == 'undefined' || ids == ""){
        alert("请选择结算商品！");
        return;
    }
    var payStatus = $(".cartLa > input:radio:checked").val();
    if(typeof payStatus == 'undefined' || payStatus == ""){
        alert("请选择支付金额！");
        return;
    }
    var payType = $(".cartPayType > input:radio:checked").val();
    if(typeof payType == 'undefined' || payType == ""){
        alert("请选择支付方式！");
        return;
    }
    $.ajax({
        type: 'get',
        url: '/loginWeb/isLogin.json',
        dataType: 'json',
        data: {},
        success: function (ret) {
            if (ret.isLogin == 'OK') {
                $.post("/htmlWeb/submitOrder",{"ids": ids,"pay_status" : payStatus,"payFor" : payType},function(data, textStatus, jqXHR){
                    window.open("htmlWeb/orderPay?order_id=" + data + "&status=" + payStatus+ "&payFor=" + payType);
                    setTimeout(function(){
                        window.location.href = "/systemMainHtml?flag='order'";
                    },500);

                });

            } else {
                navPills('loginHtml');
            }
        },
        error: function(e) {
            console.log('error')
        }
    });

}
